﻿using System;
using System.Reflection;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;
using YJKSparseBreadth.ModelLayer;
using YJKSparseBreadth.Properties;

namespace YJKSparseBreadth
{
	public static class Utility
	{
		public static double ValueZero
		{
			get
			{
				return Utility.m_ValueZero;
			}
			private set
			{
				Utility.m_ValueZero = value;
			}
		}

		public static Schema GetSchema(Autodesk.Revit.DB.Document doc, string schemaGuid)
		{
			Guid guid = new Guid(schemaGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				Transaction transaction = new Transaction(doc, "CreateSchema");
				try
				{
					transaction.Start();
					SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
					Type typeFromHandle = typeof(InsertData);
					schemaBuilder.SetSchemaName(Resources.Str_SchemaName);
					foreach (FieldInfo fieldInfo in typeFromHandle.GetFields())
					{
						if (fieldInfo.FieldType == typeof(string))
						{
							schemaBuilder.AddSimpleField(fieldInfo.Name, typeof(string));
						}
					}
					schema = schemaBuilder.Finish();
					transaction.Commit();
				}
				catch (Exception)
				{
					if (transaction.HasStarted())
					{
						transaction.RollBack();
					}
				}
			}
			return schema;
		}

		private static double m_ValueZero;
	}
}
